/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.domain;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ripper
 */
public class GameLoop implements Runnable
{
    private ClientDomainController cdc;
    private ClientGameSession cgs;
    
    private boolean running;
    
    private List<String> messagesToServer;
    
    public GameLoop(ClientDomainController domcon, ClientGameSession session) 
    {
        cdc = domcon;
        cgs = session;
        
        running = true;
        messagesToServer = new ArrayList<String>();
    }
    
    @Override
    public void run()
    {
        long lastupdate = 0;
        while(running)
        {
            
            {
                cdc.updateWindow();
                
                String srvmsg = "getsessiondata";
                
                while(messagesToServer.size() > 0)
                {
                    srvmsg += ";" + messagesToServer.remove(0);
                }
                
                String response = cdc.askTheServer(srvmsg);
                //System.out.println(response);
                
                cgs.processInformation(response);
            }
            
            try
            {
                long tts = 10 - (System.currentTimeMillis() - lastupdate);
                if(tts < 0)
                    tts = 0;
                
                Thread.sleep(tts);
            }catch(Exception ex) {}
            
            lastupdate = System.currentTimeMillis();
        }
    }
    
    public void addMessageToServer(String msg)
    {
        messagesToServer.add(msg);
    }
    public void stopRunning()
    {
        running = false;
    }
}
